package com.itheima.blog.admin.service.impl;

import com.itheima.api.client.UserDemoClient;
import com.itheima.api.client.vo.ProvinceBLogNum;
import com.itheima.api.client.vo.TBlogByCategoryID;
import com.itheima.api.client.vo.TBlogByTagID;
import com.itheima.blog.admin.domain.po.TCategory;
import com.itheima.blog.admin.dao.TagMapper;

import com.itheima.blog.admin.entity.Tag;
import com.itheima.blog.admin.dao.TCategoryMapper;
import com.itheima.blog.admin.service.IIndexService;
import com.itheima.api.client.vo.BlogContributionVO;
import com.itheima.blog.admin.vo.SummaryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class IndexService implements IIndexService{
    private final UserDemoClient userDemoClient;
    private final TagMapper tagMapper;
    private final TCategoryMapper tCategoryMapper;
    @Override
    public SummaryVo getSummary() {
        Long blogs = userDemoClient.getBlogs();
        Long comments = userDemoClient.getComments();
        Integer a = blogs.intValue();
        Integer b = comments.intValue();
        SummaryVo summaryVo = new SummaryVo();
        summaryVo.setBlogNum(a);
        summaryVo.setCommentNum(b);
        return summaryVo;
    }

    @Override
    public List<TBlogByTagID> getTBlogByTagID() {
        List<TBlogByTagID> tBlogByTagID = userDemoClient.getTBlogByTagID();
        for (int i = 0; i < tBlogByTagID.size(); i++) {
            if(tBlogByTagID.get(i).getTagId()==0||tBlogByTagID.get(i).getTagId()==null){
                tBlogByTagID.remove(i);
                i--;
                continue;
            }
            Tag tag = tagMapper.selectById(tBlogByTagID.get(i).getTagId());
            tBlogByTagID.get(i).setContent(tag.getContent());
        }

        return tBlogByTagID;
    }

    @Override
    public List<TBlogByCategoryID> getTBlogByCategoryID() {
        List<TBlogByCategoryID> tBlogByCategoryID = userDemoClient.getTBlogByCategoryID();
        for (int i = 0; i < tBlogByCategoryID.size(); i++) {
            if(tBlogByCategoryID.get(i).getCategoryId()==null||tBlogByCategoryID.get(i).getCategoryId()==0){
                tBlogByCategoryID.remove(i);
                i--;
                continue;
            }
            TCategory tCategory = tCategoryMapper.selectById(tBlogByCategoryID.get(i).getCategoryId());
            tBlogByCategoryID.get(i).setCategoryName(tCategory.getName());
        }
//        for (TBlogByCategoryID blogByCategoryID : tBlogByCategoryID) {
//            TCategory tCategory = tCategoryMapper.selectById(blogByCategoryID.getCategoryId());
//            blogByCategoryID.setCategoryName(tCategory.getName());
//        }
        return tBlogByCategoryID;
    }

    @Override
    public List<BlogContributionVO> getBlogContributions() {
        List<BlogContributionVO> blogContributions = userDemoClient.getBlogContributions();
        return blogContributions;
    }

    @Override
    public List<ProvinceBLogNum> getProvinceBLogNum(Integer classify) {
        List<ProvinceBLogNum> provinceBLogNum = userDemoClient.getProvinceBLogNum(classify);
        for (int i = 0; i < provinceBLogNum.size(); i++) {
            if(provinceBLogNum.get(i).getProvince()==null||"0".equals(provinceBLogNum.get(i).getProvince())){
                provinceBLogNum.remove(i);
                i--;
                continue;
            }
        }
        return provinceBLogNum;
    }
}
